<?php
/**
 * @title 500. 键盘行 - 哈希
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[] $words
     * @return String[]
     */
    function findWords($words) {
        /**
         * @since 2021/11/01 键盘上每个字母对应的行
         */
        $charArray = [
            "q" => 1,
            "Q" => 1,
            "w" => 1,
            "W" => 1,
            "e" => 1,
            "E" => 1,
            "r" => 1,
            "R" => 1,
            "t" => 1,
            "T" => 1,
            "y" => 1,
            "Y" => 1,
            "u" => 1,
            "U" => 1,
            "i" => 1,
            "I" => 1,
            "o" => 1,
            "O" => 1,
            "p" => 1,
            "P" => 1,
            "a" => 2,
            "A" => 2,
            "s" => 2,
            "S" => 2,
            "d" => 2,
            "D" => 2,
            "f" => 2,
            "F" => 2,
            "g" => 2,
            "G" => 2,
            "h" => 2,
            "H" => 2,
            "j" => 2,
            "J" => 2,
            "k" => 2,
            "K" => 2,
            "l" => 2,
            "L" => 2,
            "z" => 3,
            "Z" => 3,
            "x" => 3,
            "X" => 3,
            "c" => 3,
            "C" => 3,
            "v" => 3,
            "V" => 3,
            "b" => 3,
            "B" => 3,
            "n" => 3,
            "N" => 3,
            "m" => 3,
            "M" => 3,
        ];

        /**
         * @since 2021/11/01 结果集
         */
        $result = [];

        /**
         * @since 2021/11/01 判断单词
         */
        foreach ($words as $word){
            $line = $charArray[$word[0]];

            /**
             * @since 2021/11/01 单词其他字符判断
             */
            $push = true;
            for($i=1; $i<strlen($word); $i++){
                if($line != $charArray[$word[$i]]){
                    $push = false;
                    break;
                } else {}
            }

            /**
             * @since 2021/11/01 是否一行
             */
            if($push){
                $result[] = $word;
            } else {}
        }


        /**
         * @return
         */
        return $result;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202110/2021.10.31.php
 */
$datas = [
    [["Hello","Alaska","Dad","Peace"]],
    [["omk"]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->findWords(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
